package com.ccjq.jq173.shosestore.shoppingcart.service.impl;

import com.ccjq.jq173.shosestore.core.db.manager.DbConnection;
import com.ccjq.jq173.shosestore.core.util.UuidUtils;
import com.ccjq.jq173.shosestore.order.service.OrderService;
import com.ccjq.jq173.shosestore.order.service.impl.OrderServiceImpl;
import com.ccjq.jq173.shosestore.product.po.Product;
import com.ccjq.jq173.shosestore.product.service.impl.productServiceImpl;
import com.ccjq.jq173.shosestore.product.service.productService;
import com.ccjq.jq173.shosestore.shoppingcart.dao.ShoppingcartDao;
import com.ccjq.jq173.shosestore.shoppingcart.dao.impl.ShoppingcartDaoImpl;
import com.ccjq.jq173.shosestore.shoppingcart.po.ShoppingCart;
import com.ccjq.jq173.shosestore.shoppingcart.service.ShoppingcartService;
import oracle.jdbc.driver.DBConversion;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;


public class ShoppingcartServiceImpl implements ShoppingcartService
{
    private ShoppingcartDao shopdao;
    @Override
    public List<ShoppingCart> queryShoes(String userid)
    {
        shopdao = new ShoppingcartDaoImpl();
        List<ShoppingCart>  list = shopdao.queryAllCarShoes(userid);
        for(int i=0;i<list.size();i++)
        {
            ShoppingCart shoppingCart=list.get(i);
            String productId=shoppingCart.getProductId();
            Product p=new productServiceImpl().getProduct(productId);
            shoppingCart.setPrice(p.getPrice());
            list.set(i,shoppingCart);
        }
        return list;
    }

    @Override
    public boolean addCar(String productId, String userId, int quantity) {
        boolean b =false;
        shopdao = new ShoppingcartDaoImpl();
        ShoppingCart cart = shopdao.queryCar(productId,userId);
        if(cart==null)   //如果购物车里没添加过信息，就添加
        {
            System.out.println("如果购物车里没添加过信息，就添加");
            System.out.println("在Service里：");
            System.out.println("productId:"+productId);
            System.out.println("quantity:"+quantity);
            System.out.println("userid:"+userId);
            b= shopdao.addCar(productId,quantity,userId);
        }
        else            //有同名信息，就更新
        {
            System.out.println("之前添加过，有同名购物车，就覆盖");
            b= shopdao.updateCar(cart.getShoppingCartId(),quantity+cart.getQuantity());
        }
        return b;
    }

    @Override
    public boolean removeCarShoes(String shoppingCartId) {
        boolean b =false;
        shopdao = new ShoppingcartDaoImpl();
        b=shopdao.removeCarShoes(shoppingCartId);
        return b;  //true成功，false失败   ------>成功再查寻，失败弹窗
    }

    @Override
    public boolean update(String shoppingCartId, int quantity) {
        boolean b =false;
        shopdao = new ShoppingcartDaoImpl();
        b = shopdao.updateCar(shoppingCartId,quantity);
        return b;
    }

    @Override
    public boolean jieSuan(String name, String address, String sheng,
                           String youbian, String city, String phone,
                           String qu, String countmoney,int payment,String userId)
    {   //先添加订单关联表，添加订单，并且删除订单所对应的的购物信息(用之前的库存量-购买后的数量)
        boolean b=false;
        boolean b1=false;
        boolean b2 =false;
        boolean b3 = false;
        Connection con = DbConnection.getConnection();
        OrderService od = new OrderServiceImpl();
        String uuid = UuidUtils.generateShotUUID("O",10);
        shopdao = new ShoppingcartDaoImpl();
        try
        {
            con.setAutoCommit(false);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        List<ShoppingCart> list = new ShoppingcartServiceImpl().queryShoes(userId);  //查出购物车里总共有几个商品
        //System.out.println("购物车里有几件呢？="+list.size());
        String zhushi="";
        for(int i=0;i<list.size() ; i++)
        {
            System.out.println("list.get(i).getPrice()="+list.get(i).getPrice());
            b = od.addOrderProduct(uuid,list.get(i).getProductId(),list.get(i).getQuantity(),list.get(i).getPrice(),con);  //添加订单商品关联表
            if(b)
            {
                //用查出来的库存量减去添加购物车里订单的购买数量
                productService ps = new productServiceImpl();
                b1 =ps.updateStock(list.get(i).getQuantity(),list.get(i).getProductId(),con);
                if(b1)
                {
                    b2 = shopdao.shanchuCarShoes(list.get(i).getShoppingCartId(),userId,con); //删除购物车里添加的这个信息(刚添加的)
                    if(b2)
                    {
                        System.out.println("删除成功");
                    }
                    else
                        zhushi="删除失败订单所对应的购物车失败";
                }
                else
                    zhushi="更改库存量失败";//更改库存量失败就回滚
            }
            else
                zhushi="添加失败";

            if(zhushi!="")  ///添加失败就回滚
            {
                try
                {
                    System.out.println(zhushi); //打印注释
                    con.rollback();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
        //全部成功，就提交
        try
        {
            System.out.println("全部成功，提交");
            con.commit();
            con.setAutoCommit(true);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("orderId",uuid);
        map.put("payment",payment);
        map.put("amount",Double.parseDouble(countmoney));
        map.put("receiver",name);
        map.put("province",sheng);
        map.put("city",city);
        map.put("county",qu);
        map.put("address",address);
        map.put("zipcode",youbian);
        map.put("telphone",phone);
        map.put("userId",userId);
        b3 = od.createOrder(map);  //添加订单
        System.out.println("b3="+b3);
        DbConnection.close(con);
        return b3; //返回true 、false
    }

    @Override
    public boolean shanchuCarShoes(String shoppingCarId, String userId,Connection con)
    {
        boolean b =false;
        shopdao = new ShoppingcartDaoImpl();
        b = shopdao.shanchuCarShoes(shoppingCarId,userId,con);
        return b;
    }

    @Override
    public ShoppingCart queryCar(String productId, String userId) {
        return new ShoppingcartDaoImpl().queryCar(productId,userId);
    }
}
